/**
 *  确认弹窗装饰器
 * @param {*} name 弹窗的名字
 * @param {*} prop 弹窗字段名
 * @param {*} options 弹窗参数
 * @returns
 */

// 默认参数
const options = { show: 'formVisible', title: "formTitle", state: 'formState' }

// 添加和编辑弹窗
export default function (name = '', prop = "id", option = options) {
  return function (target, propertyKey, descriptor) {
    const method = descriptor.value;
    const formState = data => (data ? { value: 'edit', label: `编辑${name}` } : { value: 'add', label: `新增${name}` });
    descriptor.value = function (...args) {
      const that = this._isVue ? this : target;
      const item = args[0] || {}
      const isEdit = Boolean(item[prop])
      const current = formState(isEdit)

      that[option.show] = true;
      that[option.state] = current.value;
      that[option.title] = current.label;
      method.apply(that, [item, isEdit])
    };
  };
}

